﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RegOnline.RegOnlineService;
using System.IO;
using System.Text;

namespace RegOnline
{
    public partial class Home : Page
    {
        bool studentloadeds = false;
        bool prepodloadeds = false;

        MainViewModel ac;
        public Home()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ac = ((((Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).Controller;
            ac.PrepodsLoaded += new EventHandler(ac_PrepodsLoaded);
            ac.StudentLoaded += new EventHandler(ac_StudentLoaded);

            ac.LoadPrepods();
            ac.LoadStudents();
        }

        void ac_StudentLoaded(object sender, EventArgs e)
        {
            personComboBox1.ItemsSource = (e as GetStudentsCompletedEventArgs).Result;
            personComboBox1.ItemsSource = ac.Students;
            studentloadeds = true;
            MakeVisible();

        }

        void ac_PrepodsLoaded(object sender, EventArgs e)
        {
            personComboBox.ItemsSource = (e as GetPrepodsCompletedEventArgs).Result;
            personComboBox.ItemsSource = ac.Predods;
            prepodloadeds = true;
            MakeVisible();
        }

        void MakeVisible()
        {
            if (!studentloadeds) return;
            if (!prepodloadeds) return;
            LayoutRoot.Visibility = System.Windows.Visibility.Visible;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ac.SetUser(personComboBox.SelectedItem);

            NavigationService.Navigate(new Uri("/PrepodavatelPage", UriKind.Relative));
            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ac.SetUser(personComboBox1.SelectedItem);

            NavigationService.Navigate(new Uri("/StudPage", UriKind.Relative));

            
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog SFD = new SaveFileDialog();
            SFD.Filter = " HTML - страницы|*.html";
            SFD.FilterIndex = 1;
            PrintHtmlForms pf = new PrintHtmlForms();


            if ((bool)SFD.ShowDialog())
            {
                using (StreamWriter SW = new StreamWriter(SFD.OpenFile(), Encoding.UTF8))
                {

                    SW.Write(pf.GenerateTitul());

                    SW.Flush();
                }

            }
        }

        private void personComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}